package com.qqb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qqb.dao.CompanyDao;
import com.qqb.dao.OfficerDao;
import com.qqb.dao.PaperTypeDao;
import com.qqb.pojo.Company;
import com.qqb.service.CompanyService;
import com.qqb.service.EduService;
import com.qqb.service.PaperTypeService;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyDao companyDao;

    @Resource
    private OfficerDao officerDao;

    String now = LocalDate.now().toString();

    /**
     * @methodsName: searchPageList
     * @description: 分页信息展示+查询
     * @param: condition pageNum pageSize
     * @return: page
     */
    @Override
    public Page<Company> searchPageList(Map<String, String> condition, String pageNum, String pageSize) {

        int page_Num = Integer.parseInt(pageNum);
        int page_Size = Integer.parseInt(pageSize);

        Page<Company> page = PageHelper.startPage(page_Num, page_Size);
            Example example = createExample(condition);
            companyDao.selectByExample(example);
        /*    for (Company company : page) {
                String paperName = paperTypeService.findNameByCode(company.getPaperType());
                company.setPaperType(paperName);
            }*/

        return page;
    }

    /**
     * @methodsName: add
     * @description: 添加一个企业的信息
     * @param: company
     */
    @Transactional
    @Override
    public void add(Company company) throws Exception {
        String maxCompanyIdStr = companyDao.getMaxCompanyId();//获得数据库中企业编号的最大值

        Integer num = companyDao.getNum(company.getPaperNo(),company.getPaperType(),company.getComName());
        if (num >= 1) {
            throw new Exception("已存在该信息");
        }
        Integer maxCompanyId = Integer.parseInt(maxCompanyIdStr.replace("C", "")) + 1;//对获取到的值进行处理：去掉最前面的C将数值中的有效位提取出来再将数值加一
        StringBuilder maxId = new StringBuilder("C" + maxCompanyId);//将处理好的数值前面加上C
        /*
        判断中间C与有效位的数值中间0的个数
         */
        int maxIdLength = maxId.length();
        for (int i = 0; i < 10 - maxIdLength + 1; i++) {
            maxId.insert(1, "0");
        }
        company.setUpdateDate(now);
        company.setSetDate(now);
        company.setRegDate(now);
        company.setCid(maxId.toString());//将处理好的值变成String类型

        companyDao.insert(company);//调用insert方法添加企业信息
    }


    /**
     * @methodsName: update
     * @description: 更新公司信息
     * @param: company
     */
    @Transactional
    @Override
    public void update(Company company) throws Exception {
        //company.setPaperType(companyDao.getPtNum(company.getPaperType()));
//        Integer num = companyDao.getNum(company.getPaperNo(),company.getPaperType(),company.getComName());
//        if (num >= 1) {
//            throw new Exception("已存在该信息");
//        }
        company.setUpdateDate(now);
     /*   if (company.getPaperType() != null) {
            String paperName = paperTypeService.findNameByCode(company.getPaperType());
            company.setPaperType(paperName);
        }*/
        companyDao.updateByPrimaryKeySelective(company);
    }

    /**
     * @methodsName: delete
     * @description: 删除一个公司信息
     * @param: ids
     */
    @Transactional
    @Override
    public void delete(String[] ids) {
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("cid", Arrays.asList(ids));
        companyDao.deleteByExample(example);//删除公司信息
        officerDao.deleteByExample(example);//删除公司信息关联的高管信息
    }

    /**
     * @methodsName: createExample
     * @description: 创建一个条件合集
     * @param: condition
     * @return: example
     */
    public Example createExample(Map<String, String> condition) {

        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();

        if (condition != null && !condition.isEmpty()) {
            if (condition.get("cid") != null && !"".equals(condition.get("cid"))) {
                criteria.andEqualTo("cid", condition.get("cid"));
            }
            if (condition.get("comName") != null && !"".equals(condition.get("comName"))) {
                criteria.andLike("comName", "%" + condition.get("comName") + "%");
            }
            if (condition.get("paperType") != null && !"".equals(condition.get("paperType"))) {
                criteria.andEqualTo("paperType", condition.get("paperType"));
            }
            if (condition.get("paperNo") != null && !"".equals(condition.get("paperNo"))) {
                criteria.andEqualTo("paperNo", condition.get("paperNo"));
            }
            if (condition.get("setDate") != null && !"".equals(condition.get("setDate"))) {
                criteria.andEqualTo("setDate", condition.get("setDate"));
            }
            if (condition.get("regCapital") != null && !"".equals(condition.get("regCapital"))) {
                criteria.andEqualTo("regCapital", condition.get("regCapital"));
            }
            if (condition.get("address") != null && !"".equals(condition.get("address"))) {
                criteria.andEqualTo("address", condition.get("address"));
            }
            if (condition.get("workerNum") != null && !"".equals(condition.get("workerNum"))) {
                criteria.andEqualTo("workerNum", condition.get("workerNum"));
            }
            if (condition.get("updateDate") != null && !"".equals(condition.get("updateDate"))) {
                criteria.andEqualTo("updateDate", condition.get("updateDate"));
            }
            if (condition.get("regDate") != null && !"".equals(condition.get("regDate"))) {
                criteria.andEqualTo("regDate", condition.get("regDate"));
            }
        }
        return example;
    }
}
